<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 7/10/17
 * Time: 6:54 PM
 */
declare(strict_types=1);


namespace application\module\admin\library\component;

use application\module\admin\library\Component;

/**
 * Class Datatable
 *
 * @property string|array $config
 * @property string $id 表格的ID
 * @property string $title 表格的标题
 * @property string $width 表格的宽度
 * @property string $toolbar 工具栏
 *
 * @property string $innerHTML
 *
 * @package application\module\admin\library\component
 */
class Datatable extends Component
{

    public function __toString()
    {
        $content = '<section class="content"><div class="row">';
        if ($this->config) {
            $config = json_decode($this->config, true);
        } else {
            $config = [
                [
                    'id' => $this->id??'',
                    'title' => $this->title??'',
                    'width' => $this->width??'',
                    'toolbar' => $this->innerHTML??'',
                ]
            ];
        }

        foreach ($config as $item) {
            $boxbody = empty($item['body']) ?
                "<table id=\"{$item['id']}\" class=\"table table-bordered table-striped\"></table>" :
                $item['body'];


            $toolbar = empty($item['toolbar']) ? '' : '<div class="naz-toolbar">' . ($item['toolbar']) . '</div><hr>';

            $content .= <<<endline
<div class="col-sm-{$item['width']}">
    <div class="box">
        <div class="box-header">
            <h3 class="box-title">{$item['title']}</h3>
        </div>
        <div class="box-body"> 
        {$toolbar}
        {$boxbody} 
        </div>
    </div>
</div>
endline;
        }
        return $content . '</div></section>';
    }
}